<!DOCTYPE html><html><head>
<title>math::bignum - Tcl Math Library</title>
<style type="text/css"><!--
    HTML {
	background: 	#FFFFFF;
	color: 		black;
    }
    BODY {
	background: 	#FFFFFF;
	color:	 	black;
    }
    DIV.doctools {
	margin-left:	10%;
	margin-right:	10%;
    }
    DIV.doctools H1,DIV.doctools H2 {
	margin-left:	-5%;
    }
    H1, H2, H3, H4 {
	margin-top: 	1em;
	font-family:	sans-serif;
	font-size:	large;
	color:		#005A9C;
	background: 	transparent;
	text-align:		left;
    }
    H1.doctools_title {
	text-align: center;
    }
    UL,OL {
	margin-right: 0em;
	margin-top: 3pt;
	margin-bottom: 3pt;
    }
    UL LI {
	list-style: disc;
    }
    OL LI {
	list-style: decimal;
    }
    DT {
	padding-top: 	1ex;
    }
    UL.doctools_toc,UL.doctools_toc UL, UL.doctools_toc UL UL {
	font:		normal 12pt/14pt sans-serif;
	list-style:	none;
    }
    LI.doctools_section, LI.doctools_subsection {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding: 	0em;
    }
    PRE {
	display: 	block;
	font-family:	monospace;
	white-space:	pre;
	margin:		0%;
	padding-top:	0.5ex;
	padding-bottom:	0.5ex;
	padding-left:	1ex;
	padding-right:	1ex;
	width:		100%;
    }
    PRE.doctools_example {
	color: 		black;
	background: 	#f5dcb3;
	border:		1px solid black;
    }
    UL.doctools_requirements LI, UL.doctools_syntax LI {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding:	0em;
    }
    DIV.doctools_synopsis {
	color: 		black;
	background: 	#80ffff;
	border:		1px solid black;
	font-family:	serif;
	margin-top: 	1em;
	margin-bottom: 	1em;
    }
    UL.doctools_syntax {
	margin-top: 	1em;
	border-top:	1px solid black;
    }
    UL.doctools_requirements {
	margin-bottom: 	1em;
	border-bottom:	1px solid black;
    }
--></style>
</head>
<!-- Generated from file 'bignum.man' by tcllib/doctools with format 'html'
   -->
<!-- Copyright &amp;copy; 2004 Salvatore Sanfilippo &amp;lt;antirez at invece dot org&amp;gt;   -- Copyright &amp;copy; 2004 Arjen Markus &amp;lt;arjenmarkus at users dot sourceforge dot net&amp;gt;
   -->
<!-- math::bignum.n
   -->
<body><hr> [
   <a href="../../../../../../../../home">Tcllib Home</a>
&#124; <a href="../../../../toc.html">Main Table Of Contents</a>
&#124; <a href="../../../toc.html">Table Of Contents</a>
&#124; <a href="../../../../index.html">Keyword Index</a>
&#124; <a href="../../../../toc0.html">Categories</a>
&#124; <a href="../../../../toc1.html">Modules</a>
&#124; <a href="../../../../toc2.html">Applications</a>
 ] <hr>
<div class="doctools">
<h1 class="doctools_title">math::bignum(n) 3.1 tcllib &quot;Tcl Math Library&quot;</h1>
<div id="name" class="doctools_section"><h2><a name="name">Name</a></h2>
<p>math::bignum - Arbitrary precision integer numbers</p>
</div>
<div id="toc" class="doctools_section"><h2><a name="toc">Table Of Contents</a></h2>
<ul class="doctools_toc">
<li class="doctools_section"><a href="#toc">Table Of Contents</a></li>
<li class="doctools_section"><a href="#synopsis">Synopsis</a></li>
<li class="doctools_section"><a href="#section1">Description</a></li>
<li class="doctools_section"><a href="#section2">EXAMPLES</a></li>
<li class="doctools_section"><a href="#section3">API</a></li>
<li class="doctools_section"><a href="#section4">Bugs, Ideas, Feedback</a></li>
<li class="doctools_section"><a href="#keywords">Keywords</a></li>
<li class="doctools_section"><a href="#category">Category</a></li>
<li class="doctools_section"><a href="#copyright">Copyright</a></li>
</ul>
</div>
<div id="synopsis" class="doctools_section"><h2><a name="synopsis">Synopsis</a></h2>
<div class="doctools_synopsis">
<ul class="doctools_requirements">
<li>package require <b class="pkgname">Tcl <span class="opt">?8.4?</span></b></li>
<li>package require <b class="pkgname">math::bignum <span class="opt">?3.1?</span></b></li>
</ul>
<ul class="doctools_syntax">
<li><a href="#1"><b class="cmd">::math::bignum::fromstr</b> <i class="arg">string</i> ?<i class="arg">radix</i>?</a></li>
<li><a href="#2"><b class="cmd">::math::bignum::tostr</b> <i class="arg">bignum</i> ?<i class="arg">radix</i>?</a></li>
<li><a href="#3"><b class="cmd">::math::bignum::sign</b> <i class="arg">bignum</i></a></li>
<li><a href="#4"><b class="cmd">::math::bignum::abs</b> <i class="arg">bignum</i></a></li>
<li><a href="#5"><b class="cmd">::math::bignum::cmp</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#6"><b class="cmd">::math::bignum::iszero</b> <i class="arg">bignum</i></a></li>
<li><a href="#7"><b class="cmd">::math::bignum::lt</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#8"><b class="cmd">::math::bignum::le</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#9"><b class="cmd">::math::bignum::gt</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#10"><b class="cmd">::math::bignum::ge</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#11"><b class="cmd">::math::bignum::eq</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#12"><b class="cmd">::math::bignum::ne</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#13"><b class="cmd">::math::bignum::isodd</b> <i class="arg">bignum</i></a></li>
<li><a href="#14"><b class="cmd">::math::bignum::iseven</b> <i class="arg">bignum</i></a></li>
<li><a href="#15"><b class="cmd">::math::bignum::add</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#16"><b class="cmd">::math::bignum::sub</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#17"><b class="cmd">::math::bignum::mul</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#18"><b class="cmd">::math::bignum::divqr</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#19"><b class="cmd">::math::bignum::div</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#20"><b class="cmd">::math::bignum::rem</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#21"><b class="cmd">::math::bignum::mod</b> <i class="arg">n</i> <i class="arg">m</i></a></li>
<li><a href="#22"><b class="cmd">::math::bignum::pow</b> <i class="arg">base</i> <i class="arg">exp</i></a></li>
<li><a href="#23"><b class="cmd">::math::bignum::powm</b> <i class="arg">base</i> <i class="arg">exp</i> <i class="arg">m</i></a></li>
<li><a href="#24"><b class="cmd">::math::bignum::sqrt</b> <i class="arg">bignum</i></a></li>
<li><a href="#25"><b class="cmd">::math::bignum::rand</b> <i class="arg">bits</i></a></li>
<li><a href="#26"><b class="cmd">::math::bignum::lshift</b> <i class="arg">bignum</i> <i class="arg">bits</i></a></li>
<li><a href="#27"><b class="cmd">::math::bignum::rshift</b> <i class="arg">bignum</i> <i class="arg">bits</i></a></li>
<li><a href="#28"><b class="cmd">::math::bignum::bitand</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#29"><b class="cmd">::math::bignum::bitor</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#30"><b class="cmd">::math::bignum::bitxor</b> <i class="arg">a</i> <i class="arg">b</i></a></li>
<li><a href="#31"><b class="cmd">::math::bignum::setbit</b> <i class="arg">bignumVar</i> <i class="arg">bit</i></a></li>
<li><a href="#32"><b class="cmd">::math::bignum::clearbit</b> <i class="arg">bignumVar</i> <i class="arg">bit</i></a></li>
<li><a href="#33"><b class="cmd">::math::bignum::testbit</b> <i class="arg">bignum</i> <i class="arg">bit</i></a></li>
<li><a href="#34"><b class="cmd">::math::bignum::bits</b> <i class="arg">bignum</i></a></li>
</ul>
</div>
</div>
<div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2>
<p>The bignum package provides arbitrary precision integer math
(also known as &quot;big numbers&quot;) capabilities to the Tcl language.
Big numbers are internally represented at Tcl lists: this
package provides a set of procedures operating against
the internal representation in order to:</p>
<ul class="doctools_itemized">
<li><p>perform math operations</p></li>
<li><p>convert bignums from the internal representation to a string in
the desired radix and vice versa.</p></li>
</ul>
<p>But the two constants &quot;0&quot; and &quot;1&quot; are automatically converted to
the internal representation, in order to easily compare a number to zero,
or increment a big number.</p>
<p>The bignum interface is opaque, so
operations on bignums that are not returned by procedures
in this package (but created by hand) may lead to unspecified behaviours.
It's safe to treat bignums as pure values, so there is no need
to free a bignum, or to duplicate it via a special operation.</p>
</div>
<div id="section2" class="doctools_section"><h2><a name="section2">EXAMPLES</a></h2>
<p>This section shows some simple example. This library being just
a way to perform math operations, examples may be the simplest way
to learn how to work with it. Consult the API section of
this man page for information about individual procedures.</p>
<pre class="doctools_example">
    package require math::bignum
    # Multiplication of two bignums
    set a [::math::bignum::fromstr 88888881111111]
    set b [::math::bignum::fromstr 22222220000000]
    set c [::math::bignum::mul $a $b]
    puts [::math::bignum::tostr $c] ; # =&gt; will output 1975308271604953086420000000
    set c [::math::bignum::sqrt $c]
    puts [::math::bignum::tostr $c] ; # =&gt; will output 44444440277777
    # From/To string conversion in different radix
    set a [::math::bignum::fromstr 1100010101010111001001111010111 2]
    puts [::math::bignum::tostr $a 16] ; # =&gt; will output 62ab93d7
    # Factorial example
    proc fact n {
        # fromstr is not needed for 0 and 1
        set z 1
        for {set i 2} {$i &lt;= $n} {incr i} {
            set z [::math::bignum::mul $z [::math::bignum::fromstr $i]]
        }
        return $z
    }
    puts [::math::bignum::tostr [fact 100]]
</pre>
</div>
<div id="section3" class="doctools_section"><h2><a name="section3">API</a></h2>
<dl class="doctools_definitions">
<dt><a name="1"><b class="cmd">::math::bignum::fromstr</b> <i class="arg">string</i> ?<i class="arg">radix</i>?</a></dt>
<dd><p>Convert <em>string</em> into a bignum. If <em>radix</em> is omitted or zero,
the string is interpreted in hex if prefixed with
<em>0x</em>, in octal if prefixed with <em>ox</em>,
in binary if it's pefixed with <em>bx</em>, as a number in
radix 10 otherwise. If instead the <em>radix</em> argument
is specified in the range 2-36, the <em>string</em> is interpreted
in the given radix. Please note that this conversion is
not needed for two constants : <em>0</em> and <em>1</em>. (see the example)</p></dd>
<dt><a name="2"><b class="cmd">::math::bignum::tostr</b> <i class="arg">bignum</i> ?<i class="arg">radix</i>?</a></dt>
<dd><p>Convert <em>bignum</em> into a string representing the number
in the specified radix. If <em>radix</em> is omitted, the
default is 10.</p></dd>
<dt><a name="3"><b class="cmd">::math::bignum::sign</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return the sign of the bignum.
The procedure returns 0 if the number is positive, 1 if it's negative.</p></dd>
<dt><a name="4"><b class="cmd">::math::bignum::abs</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return the absolute value of the bignum.</p></dd>
<dt><a name="5"><b class="cmd">::math::bignum::cmp</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Compare the two bignums a and b, returning <em>0</em> if <em>a == b</em>,
<em>1</em> if <em>a &gt; b</em>, and <em>-1</em> if <em>a &lt; b</em>.</p></dd>
<dt><a name="6"><b class="cmd">::math::bignum::iszero</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return true if <em>bignum</em> value is zero, otherwise false is returned.</p></dd>
<dt><a name="7"><b class="cmd">::math::bignum::lt</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return true if <em>a &lt; b</em>, otherwise false is returned.</p></dd>
<dt><a name="8"><b class="cmd">::math::bignum::le</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return true if <em>a &lt;= b</em>, otherwise false is returned.</p></dd>
<dt><a name="9"><b class="cmd">::math::bignum::gt</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return true if <em>a &gt; b</em>, otherwise false is returned.</p></dd>
<dt><a name="10"><b class="cmd">::math::bignum::ge</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return true if <em>a &gt;= b</em>, otherwise false is returned.</p></dd>
<dt><a name="11"><b class="cmd">::math::bignum::eq</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return true if <em>a == b</em>, otherwise false is returned.</p></dd>
<dt><a name="12"><b class="cmd">::math::bignum::ne</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return true if <em>a != b</em>, otherwise false is returned.</p></dd>
<dt><a name="13"><b class="cmd">::math::bignum::isodd</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return true if <em>bignum</em> is odd.</p></dd>
<dt><a name="14"><b class="cmd">::math::bignum::iseven</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return true if <em>bignum</em> is even.</p></dd>
<dt><a name="15"><b class="cmd">::math::bignum::add</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the sum of the two bignums <em>a</em> and <em>b</em>.</p></dd>
<dt><a name="16"><b class="cmd">::math::bignum::sub</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the difference of the two bignums <em>a</em> and <em>b</em>.</p></dd>
<dt><a name="17"><b class="cmd">::math::bignum::mul</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the product of the two bignums <em>a</em> and <em>b</em>.
The implementation uses Karatsuba multiplication if both
the numbers are bigger than a given threshold, otherwise
the direct algorith is used.</p></dd>
<dt><a name="18"><b class="cmd">::math::bignum::divqr</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return a two-elements list containing as first element
the quotient of the division between the two bignums
<em>a</em> and <em>b</em>, and the remainder of the division as second element.</p></dd>
<dt><a name="19"><b class="cmd">::math::bignum::div</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the quotient of the division between the two
bignums <em>a</em> and <em>b</em>.</p></dd>
<dt><a name="20"><b class="cmd">::math::bignum::rem</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the remainder of the division between the two
bignums <em>a</em> and <em>b</em>.</p></dd>
<dt><a name="21"><b class="cmd">::math::bignum::mod</b> <i class="arg">n</i> <i class="arg">m</i></a></dt>
<dd><p>Return <em>n</em> modulo <em>m</em>. This operation is
called modular reduction.</p></dd>
<dt><a name="22"><b class="cmd">::math::bignum::pow</b> <i class="arg">base</i> <i class="arg">exp</i></a></dt>
<dd><p>Return <em>base</em> raised to the exponent <em>exp</em>.</p></dd>
<dt><a name="23"><b class="cmd">::math::bignum::powm</b> <i class="arg">base</i> <i class="arg">exp</i> <i class="arg">m</i></a></dt>
<dd><p>Return <em>base</em> raised to the exponent <em>exp</em>,
modulo <em>m</em>. This function is often used in the field
of cryptography.</p></dd>
<dt><a name="24"><b class="cmd">::math::bignum::sqrt</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return the integer part of the square root of <em>bignum</em></p></dd>
<dt><a name="25"><b class="cmd">::math::bignum::rand</b> <i class="arg">bits</i></a></dt>
<dd><p>Return a random number of at most <em>bits</em> bits.
The returned number is internally generated using Tcl's <em>expr rand()</em>
function and is not suitable where an unguessable and cryptographically
secure random number is needed.</p></dd>
<dt><a name="26"><b class="cmd">::math::bignum::lshift</b> <i class="arg">bignum</i> <i class="arg">bits</i></a></dt>
<dd><p>Return the result of left shifting <em>bignum</em>'s binary
representation of <em>bits</em> positions on the left.
This is equivalent to multiplying by 2^<em>bits</em> but much faster.</p></dd>
<dt><a name="27"><b class="cmd">::math::bignum::rshift</b> <i class="arg">bignum</i> <i class="arg">bits</i></a></dt>
<dd><p>Return the result of right shifting <em>bignum</em>'s binary
representation of <em>bits</em> positions on the right.
This is equivalent to dividing by <em>2^bits</em> but much faster.</p></dd>
<dt><a name="28"><b class="cmd">::math::bignum::bitand</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the result of doing a bitwise AND operation on a
and b. The operation is restricted to positive numbers,
including zero. When negative numbers are provided as
arguments the result is undefined.</p></dd>
<dt><a name="29"><b class="cmd">::math::bignum::bitor</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the result of doing a bitwise OR operation on a
and b. The operation is restricted to positive numbers,
including zero. When negative numbers are provided as
arguments the result is undefined.</p></dd>
<dt><a name="30"><b class="cmd">::math::bignum::bitxor</b> <i class="arg">a</i> <i class="arg">b</i></a></dt>
<dd><p>Return the result of doing a bitwise XOR operation on a
and b. The operation is restricted to positive numbers,
including zero. When negative numbers are provided as
arguments the result is undefined.</p></dd>
<dt><a name="31"><b class="cmd">::math::bignum::setbit</b> <i class="arg">bignumVar</i> <i class="arg">bit</i></a></dt>
<dd><p>Set the bit at <em>bit</em> position to 1 in the bignum stored
in the variable <em>bignumVar</em>. Bit 0 is the least significant.</p></dd>
<dt><a name="32"><b class="cmd">::math::bignum::clearbit</b> <i class="arg">bignumVar</i> <i class="arg">bit</i></a></dt>
<dd><p>Set the bit at <em>bit</em> position to 0 in the bignum stored
in the variable <em>bignumVar</em>. Bit 0 is the least significant.</p></dd>
<dt><a name="33"><b class="cmd">::math::bignum::testbit</b> <i class="arg">bignum</i> <i class="arg">bit</i></a></dt>
<dd><p>Return true if the bit at the <em>bit</em> position of <em>bignum</em>
is on, otherwise false is returned. If <em>bit</em> is out of
range, it is considered as set to zero.</p></dd>
<dt><a name="34"><b class="cmd">::math::bignum::bits</b> <i class="arg">bignum</i></a></dt>
<dd><p>Return the number of bits needed to represent bignum in radix 2.</p></dd>
</dl>
</div>
<div id="section4" class="doctools_section"><h2><a name="section4">Bugs, Ideas, Feedback</a></h2>
<p>This document, and the package it describes, will undoubtedly contain
bugs and other problems.
Please report such in the category <em>math :: bignum</em> of the
<a href="http://core.tcl.tk/tcllib/reportlist">Tcllib Trackers</a>.
Please also report any ideas for enhancements you may have for either
package and/or documentation.</p>
<p>When proposing code changes, please provide <em>unified diffs</em>,
i.e the output of <b class="const">diff -u</b>.</p>
<p>Note further that <em>attachments</em> are strongly preferred over
inlined patches. Attachments can be made by going to the <b class="const">Edit</b>
form of the ticket immediately after its creation, and then using the
left-most button in the secondary navigation bar.</p>
</div>
<div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2>
<p><a href="../../../../index.html#bignums">bignums</a>, <a href="../../../../index.html#math">math</a>, <a href="../../../../index.html#multiprecision">multiprecision</a>, <a href="../../../../index.html#tcl">tcl</a></p>
</div>
<div id="category" class="doctools_section"><h2><a name="category">Category</a></h2>
<p>Mathematics</p>
</div>
<div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2>
<p>Copyright &copy; 2004 Salvatore Sanfilippo &lt;antirez at invece dot org&gt;<br>
Copyright &copy; 2004 Arjen Markus &lt;arjenmarkus at users dot sourceforge dot net&gt;</p>
</div>
</div></body></html>
